<template>
    <div class="import-data">
        <el-form ref="form" label-width="130px">
            <div class="btn-content2">
                <el-button type="primary" @click="importData">导入</el-button>
                <el-button>取消</el-button>
            </div>
            <el-row v-if="formResult">
                <el-col
                    v-for="item in formField"
                    :key="item.fieldId"
                    :xs="24"
                    :sm="12"
                    :md="12"
                    :lg="12"
                    :xl="8">
                    <el-form-item :label="item.fieldLableName">
                        <el-select v-model="formResult[item.fieldTalbename]" value-key="fieldLableName" @change="handleChange" placeholder="请选择">
                            <el-option
                                v-for="item2 in formField"
                                :key="item2.fieldId"
                                :label="item2.fieldLableName"
                                :value="item2"/>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
    </div>
</template>

<script>
import { exportField, getImportFileHead, importExcelData, recording } from '@/api/api'
import Cookies from 'js-cookie'
const session = JSON.parse(Cookies.get('sessionData'))
export default {
    name: 'importData2',
    props: {
        importExcelData: Object,
        formField: Array,
        fileid: ''
    },
    data() {
        return {
            fieldSet: [],
            formResult: {}
        }
    },
    created() {
        this.init()
    },
    methods: {
        handleChange(e) {
            console.log(e)
        },
        init() {
            this._getImportFileHead()
        },
        _getImportFileHead() {
            getImportFileHead(this.fileid)
                .then(res => {
                    console.log(res)
                    const object = res.result
                    // this.formResult = object
                    let item2 = {
                        cell: 'cell1',
                        importfield: 'EMPCODE',
                        fieldLableName: ''
                    }
                    for (const key in object) {
                        if (object.hasOwnProperty(key)) {
                            const element = object[key]
                            this.formField.forEach(item => {
                                if (item.fieldLableName === element) {
                                    this.$set(this.formResult, item.fieldTalbename, item)
                                    // this.formResult[item.fieldTalbename] = element
                                    item2 = {
                                        cell: key,
                                        importfield: item.fieldTalbename
                                        // fieldLableName: element
                                    }
                                    this.fieldSet.push(item2)
                                }
                            })
                        }
                    }
                    console.log(this.formResult)
                    console.log(this.fieldSet)
                    console.log(this.formField)
                    this.$forceUpdate()
                })
        },
        _importExcelData(importType, params) {
            let errors = []
            importExcelData(importType, params)
                .then(res => {
                    console.log(res)
                    if (res[0] === '0') {
                        this._recording()
                    } else {
                        errors = res
                    }
                    if (errors[0] === '1') {
                        window.open(`/myehr/form/getImportErrorFile.action?path=${errors[1]}&filename=${errors[2]}`)
                    }
                })
        },
        _recording() {
            recording(this.importExcelData.formId)
                .then(res => {
                    console.log(res)
                    const datas = res
                    var INSRTNUM = datas[0].insrtnum
                    if (INSRTNUM === null) {
                        INSRTNUM = '0'
                    }
                    var UPNUM = datas[0].upnum
                    if (UPNUM === null) {
                        UPNUM = '0'
                    }
                    var ERRORNUM = datas[0].errornum
                    if (ERRORNUM === null) {
                        ERRORNUM = '0'
                    }
                    var sign = '导入成功!插入' + INSRTNUM + '条,更新' + UPNUM + '条,失败' + ERRORNUM + '条'
                    alert(sign)
                })
        },
        importData() {
            this.fieldSet.forEach(item => {
                item.importfield = this.formResult[item.importfield].fieldTalbename
            })
            var _params = {
                'formId': String(this.importExcelData.formId),
                'filePath': String(this.importExcelData.fileid),
                'clientFileName': this.importExcelData.templateFilename,
                'repeOper': '1',
                'execptOper': '1',
                'fieldSet': this.fieldSet,
                'buttonId': String(this.importExcelData.buttonId)
            }
            this.$confirm('选择导入模式', '提示', {
                confirmButtonText: '新',
                cancelButtonText: '旧',
                type: 'warning'
            }).then(() => {
                this._importExcelData('new', _params)
            }).catch(() => {
                // 旧
                this._importExcelData('old', _params)
            })
        }
    }
}
</script>
